package com.systemframework.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.systemframework.constants.Constants;
import com.systemframework.sys.dto.request.SysMenuRequestDTO;
import com.systemframework.sys.dto.response.SysMenuResponseDTO;
import com.systemframework.sys.dto.response.SysMetaRequestDTO;
import com.systemframework.sys.dto.response.SysRoleMenuResponseDTO;
import com.systemframework.sys.entity.SysMenu;
import com.systemframework.sys.mapper.SysMenuMapper;
import com.systemframework.sys.service.SysMenuService;
import com.systemframework.sys.service.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author summer
 * @date 2023-03-10 10:16
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {


    @Override
    public List<SysMenuResponseDTO> findUserAndMenuLists(Integer userId) {
        List<SysMenuResponseDTO> sysMenuResponseDTOList=this.baseMapper.findUserAndMenuLists(userId);
        return children(sysMenuResponseDTOList);
    }

    private List<SysMenuResponseDTO> children(List<SysMenuResponseDTO> sysMenuResponseDTOList){
        sysMenuResponseDTOList.forEach(sysMenuResponseDTO -> {
           SysMetaRequestDTO meta = sysMenuResponseDTO.getMeta();
           if(meta.getRoles().size()>0){
               meta.setRoles(ListUtil.toList(meta.getRoles().get(0).split(Constants.COMMA)));
           }
           if(sysMenuResponseDTO.getChildren().size()>0){
               children(sysMenuResponseDTO.getChildren());
           }
       });
        return sysMenuResponseDTOList;
    }

    @Override
    public List<SysMenuResponseDTO> lists(Integer pId) {
        LambdaQueryWrapper<SysMenu> eq = Wrappers.lambdaQuery(SysMenu.class).eq(SysMenu::getPid, pId);
        List<SysMenuResponseDTO> sysMenuResponseDTOList = BeanUtil.copyToList(this.list(eq), SysMenuResponseDTO.class);
        sysMenuResponseDTOList.forEach(sysMenuResponseDTO -> {
            if (pId.equals(0)){
                sysMenuResponseDTO.setHasChildren(true);
            }
        });
        return sysMenuResponseDTOList;
    }

    private SysRoleMenuService sysRoleMenuService;

    @Autowired
    public void setSysRoleMenuService(SysRoleMenuService sysRoleMenuService) {
        this.sysRoleMenuService = sysRoleMenuService;
    }

    @Override
    public SysRoleMenuResponseDTO menuLists(Integer roleId) {
        SysRoleMenuResponseDTO sysRoleMenuResponseDTO=new SysRoleMenuResponseDTO();
        List<Integer>  menuIds= sysRoleMenuService.findSelectedIds(roleId);
        sysRoleMenuResponseDTO.setMenuIds(menuIds);
        sysRoleMenuResponseDTO.setSysMenuResponseDTOList(this.menus());
        return sysRoleMenuResponseDTO;
    }

    @Override
    public List<SysMenuResponseDTO> menus() {
        List<SysMenuResponseDTO> sysMenuResponseDTOList = Convert.toList(SysMenuResponseDTO.class, this.list());
        return children(sysMenuResponseDTOList,Constants.ZERO);
    }

    @Override
    public void saveMenu(SysMenuRequestDTO sysMenuRequestDTO) {
        SysMenu sysMenu = Convert.convert(SysMenu.class, sysMenuRequestDTO);
        if(ObjectUtil.isEmpty(sysMenuRequestDTO.getPid())){
            sysMenu.setPid(Constants.ZERO);
        }else{
            //更新上级 redirect
            SysMenu menu = this.getById(sysMenu.getPid());
            if(StrUtil.isEmpty(menu.getRedirect())){
                menu.setRedirect("/"+menu.getName()+"/"+sysMenu.getName());
                this.updateById(menu);
            }
        }
        this.saveOrUpdate(sysMenu);
    }

    private List<SysMenuResponseDTO> children(List<SysMenuResponseDTO> sysMenuResponseDTOList,Integer pId){
        List<SysMenuResponseDTO> lists=ListUtil.list(true);
        sysMenuResponseDTOList.forEach(sysMenuResponseDTO -> {
            if(sysMenuResponseDTO.getPid().equals(pId)){
                List<SysMenuResponseDTO> children = children(sysMenuResponseDTOList, sysMenuResponseDTO.getId());
                sysMenuResponseDTO.setChildren(children);
                lists.add(sysMenuResponseDTO);
            }
        });

        return lists;
    }
}
